package smallfrench.ui;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;

/**
 * Une boite de dialogue rechercher / remplacer.
 * @author antoine1023
 *
 */
@SuppressWarnings("serial")
public class DialogueRechercher extends JDialog {

	private JTextField textFieldRechercher;
	private JTextField textFieldRemplacer;
	private JCheckBox chckbxCasse;

	private MTextPane textPane;

	public DialogueRechercher(final JFrame parent) {

		super(parent);
		setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		setResizable(false);
		setTitle("Rechercher et remplacer");
		setBounds(100, 100, 501, 196);
		getContentPane().setLayout(new BorderLayout());
		{
			Box verticalBox = Box.createVerticalBox();
			verticalBox.setBorder(BorderFactory.createEmptyBorder(
					10, 10, 10, 10));
			getContentPane().add(verticalBox, BorderLayout.NORTH);
			{
				Box hBoxRechercher = Box.createHorizontalBox();
				verticalBox.add(hBoxRechercher);

				JLabel lblRechercher = new JLabel("Rechercher : ");
				hBoxRechercher.add(lblRechercher);

				Component horizontalStrut = Box.createHorizontalStrut(20);
				hBoxRechercher.add(horizontalStrut);

				textFieldRechercher = new JTextField();
				hBoxRechercher.add(textFieldRechercher);

			}

			{
				Component verticalStrut = Box.createVerticalStrut(10);
				verticalBox.add(verticalStrut);
			}

			{
				Box hBoxRemplacer = Box.createHorizontalBox();
				verticalBox.add(hBoxRemplacer);

				JLabel lblNewLabel = new JLabel("Remplacer par : ");
				hBoxRemplacer.add(lblNewLabel);

				Component horizontalStrut = Box.createHorizontalStrut(10);
				hBoxRemplacer.add(horizontalStrut);

				textFieldRemplacer = new JTextField();
				hBoxRemplacer.add(textFieldRemplacer);
			}

			Component verticalStrut = Box.createVerticalStrut(20);
			verticalBox.add(verticalStrut);

			Box horizontalBox = Box.createHorizontalBox();
			verticalBox.add(horizontalBox);

			chckbxCasse = new JCheckBox("Respecter la casse");
			chckbxCasse.setVerticalAlignment(SwingConstants.TOP);
			chckbxCasse.setHorizontalAlignment(SwingConstants.LEFT);
			chckbxCasse.setToolTipText("Respecter la casse (le fait que les" +
					"caractères soient en majuscules ou en minuscules)");
			horizontalBox.add(chckbxCasse);

			Component horizontalGlue = Box.createHorizontalGlue();
			horizontalBox.add(horizontalGlue);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);

			JButton boutonFermer = new JButton("Fermer");
			buttonPane.add(boutonFermer);
			boutonFermer.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					setVisible(false);
				}
			});
			getRootPane().setDefaultButton(boutonFermer);

			JButton boutonToutRemplacer = new JButton("Tout remplacer");
			boutonToutRemplacer.setToolTipText(
					"Remplace toutes les occurences");
			boutonToutRemplacer.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					toutRemplacer();
				}
			});
			buttonPane.add(boutonToutRemplacer);

			JButton boutonRemplacer = new JButton("Remplacer");
			boutonToutRemplacer.setToolTipText(
					"Remplace l’occurences suivante");
			buttonPane.add(boutonRemplacer);

			JButton boutonChercher = new JButton("Rechercher");
			boutonChercher.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					rechercher();
				}
			});
			buttonPane.add(boutonChercher);

		}

	}

	//	private int getTailleSelection() {
	//		return Math.abs(textPane.getSelectionEnd()
	//				- textPane.getSelectionStart());
	//	}

	private void toutRemplacer() {

		String texte = textPane.getText(),
				texteMin = texte,
				cible = textFieldRechercher.getText(),
				remplacant = textFieldRemplacer.getText();

		if (! chckbxCasse.isSelected()) {
			texteMin = texte.toLowerCase();
			cible = cible.toLowerCase();
		}

		int pos = 0;
		for (;;) {

			pos = texteMin.indexOf(cible, pos);
			if (pos == -1) break;

			String avant = texte.substring(0, pos),
					apres = texte.substring(pos + cible.length());
			texte = avant + remplacant + apres;

			if (! chckbxCasse.isSelected())
				texteMin = texte.toLowerCase();
			else texteMin = texte;
		}

		textPane.setText(texte);
	}

	private void rechercher() {

		String texte = textPane.getText(),
				cible = textFieldRechercher.getText();

		if (! chckbxCasse.isSelected()) {
			texte = texte.toLowerCase();
			cible = cible.toLowerCase();
		}

		if (! texte.contains(cible)) {
			return;
		}

		int debutOccurence = rechercherSuivant(texte, cible,
				textPane.getSelectionStart());

		textPane.select(debutOccurence, debutOccurence + cible.length());
	}

	private int rechercherSuivant(String texte, String cible, int debut) {

		int debutOccurence = texte.indexOf(cible, debut + 1);

		if (debutOccurence == -1)
			return rechercherSuivant(texte, cible, -1);

		return debutOccurence;
	}

	public MTextPane getTextPane() {
		return textPane;
	}

	public void setTextPane(MTextPane textPane) {
		this.textPane = textPane;
	}

}
